﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
using System.Configuration;

/// <summary>
/// Summary description for Utilitys
/// </summary>
public class Utilitys
{
	public Utilitys()
	{
		//
		// TODO: Add constructor logic here
		//
	}
     public static string modHost = ConfigurationSettings.AppSettings["modHost"];
        public static string userHost = ConfigurationSettings.AppSettings["host"];
        public static int CharsPerProductName = 250;//int.Parse(ConfigurationSettings.AppSettings["Product.CharsPerProductName"]);
        public static String ConvertTo_NoSoundVietnamese(string accented)
        {
            if (accented == null || accented == "")
                return "";

            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");

            string strFormD = accented.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

        public static String RemoveSpecialChars(String str)
        {
            StringBuilder buffer = new StringBuilder(str.Length);
            foreach (char ch in str)
                if (('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || (ch == '-' || ch == '_' || ch == ' '))
                    buffer.Append(ch);
            return buffer.ToString();
        }
        public static string sefWord(String str)
        {
            if (str.Length > CharsPerProductName)
                str = str.Substring(0, CharsPerProductName);
            str = str.Trim();
            return HttpUtility.UrlEncode(RemoveSpecialChars(ConvertTo_NoSoundVietnamese(str).ToLower().Trim().Replace(' ', '-')));
        }

        public static string sefSubjectLink(int subjectID, string subjectName)
        {
            return String.Format(userHost + "/hinh-anh/{0}-{1}.html", subjectID, sefWord(subjectName.Trim()).ToLower());

        }

        public static string sefCategoryLink(int cateID, string cateName)
        {
            return String.Format(userHost + "/{0}-{1}.html", cateID, sefWord(cateName).ToLower());

        }


        public static string sefArticleLink(int articleID, string articleName)
        {
            return String.Format(userHost + "/chi-tiet-bai-viet/{0}-{1}.html", articleID, sefWord(articleName).ToLower());

        }

        public static string sefListArticleLink(string name)
        {
            return String.Format(userHost + "/bai-viet.html");

        }
        public static String clearUnicode(string accented)
        {
            if (accented == null || accented == "")
                return "";
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = accented.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

        public static String OptimzeUrl(String url)
        {
            String value = clearUnicode(url);
            if (String.IsNullOrEmpty(value))
                return "";
            else
                return System.Web.HttpUtility.UrlEncode(Regex.Replace(value, "[^a-zA-Z0-9_.]+", "-", RegexOptions.Compiled));

        }
       

        public static string ConvertToTimeVietName(DateTime datetime)
        {
            string strTime = "";
            int d = datetime.Day;
            int m = datetime.Month;
            int year = datetime.Year;
            int h = datetime.Hour;
            int minute = datetime.Minute;
            int second = datetime.Second;

            strTime = d + "/" + m + "/" + year + " " + h + ":" + minute + ":" + second;
            return strTime;
        }
}